/*
 * gnizr is a trademark of Image Matters LLC in the United States.
 * 
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License
 * for the specific language governing rights and limitations under the License.
 * 
 * The Initial Contributor of the Original Code is Image Matters LLC.
 * Portions created by the Initial Contributor are Copyright (C) 2007
 * Image Matters LLC. All Rights Reserved.
 */
package com.gnizr.web.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class ServletUtilities {

	public static String getCookieValue(Cookie[] cookies, String cookieName,
			String defaultValue) {
		if(cookies == null){
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if (cookieName.equals(cookie.getName()))
				return (cookie.getValue());
		}
		return (defaultValue);
	}

	public static Cookie getCookie(Cookie[] cookies, String cookieName) {
		if(cookies == null){
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if (cookieName.equals(cookie.getName()))
				return (cookie);
		}
		return (null);
	}
	
	public static void deleteCookie(HttpServletResponse res, String cookieName){
		Cookie cookie = new Cookie(cookieName,null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		res.addCookie(cookie);
	}
	
	public static final int SECONDS_PER_YEAR = 60 * 60 * 24 * 365;
}
